package com.click.app.dao.hibernate;

import com.click.app.dao.EntryProductDetailDao;
import com.click.app.model.Product;
import com.click.app.model.EntryProductDetail;
import java.util.Date;
import java.util.List;

/**
 * @author Joel Chuca Condo
 * @date 04/05/2009
 */
public class EntryProductDetailDaoHibernate extends GenericDaoHibernate<EntryProductDetail, Long> implements EntryProductDetailDao {

    public EntryProductDetailDaoHibernate(){
        super(EntryProductDetail.class);
    }

    public List<EntryProductDetail> findByProduct(Product product) {
        List<EntryProductDetail> entryProductsDetails = getHibernateTemplate().find("from EntryProductDetail where product=? order by entryProduct.date", product);
        /*if(!entryProductsDetails.equals(null))
            return entryProductsDetails;
        else
            return new ArrayList<EntryProductDetail>();*/
        return entryProductsDetails;
    }

    public List<EntryProductDetail> findByProduct(Long id) {
        List<EntryProductDetail> entryProductsDetails = getHibernateTemplate().find("from EntryProductDetail where product.id=? order by entryProduct.date", id);
        /*if(!entryProductsDetails.equals(null))
            return entryProductsDetails;
        else
            return new ArrayList<EntryProductDetail>();*/
        return entryProductsDetails;
    }

    public List<EntryProductDetail> findByProductPeriod(Product product, Date initDate, Date endDate) {
        Object[] objects = {product, initDate, endDate};
        List<EntryProductDetail> entryProductsDetails = getHibernateTemplate().find("from EntryProductDetail where product=? and entryProduct.date Between ? And ? order by entryProduct.date", objects);
        /*if(!entryProductsDetails.equals(null))
            return entryProductsDetails;
        else
            return new ArrayList<EntryProductDetail>();*/
        return entryProductsDetails;
    }

    public List<EntryProductDetail> findByProductPeriod(Long id, Date initDate, Date endDate) {
        Object[] objects = {id, initDate, endDate};
        List<EntryProductDetail> entryProductsDetails = getHibernateTemplate().find("from EntryProductDetail where product.id=? and entryProduct.date Between ? And ? order by entryProduct.date", objects);
        /*if(!entryProductsDetails.equals(null))
            return entryProductsDetails;
        else
            return new ArrayList<EntryProductDetail>();*/
        return entryProductsDetails;
    }
    public List<Product> findByProductPeriod(){
        return getHibernateTemplate().find("select distinct entryProductDetail.product from EntryProductDetail entryProductDetail where size(entryProductDetail.packageDetails) = 0 ");
    }
    public List<EntryProductDetail> findByProductStockNoCero(Long id){
        return getHibernateTemplate().find("from EntryProductDetail where product.id=? and currentQuantity > 0 order by entryProduct.date desc ",id);
    }

}
